//
//  VVMainViewController.swift
//  Weibo
//
//  Created by 王惠平 on 2017/3/26.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit
import SVProgressHUD

class VVMainViewController: UITabBarController, VVTabBarDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

    
        //使用kvc给只读属性赋值
        let vvTabBar = VVTabBar()
        
        //设置代理
        vvTabBar.vvDelegate = self
        
        //MARK: ---设置闭包
        vvTabBar.didSelectedComposeButtonClosure = {[weak self] in
            
//            print(self ?? 0)
//            print("我是闭包调用过来的")
            
            if !VVUserAccountViewModel.sharedViewModel.islogin {
                
                SVProgressHUD.showError(withStatus: "请先登录,亲~")
            }
            
            //代码执行到此,表示登录成功
//            let composeView = VVComposeView()
//            
//            //把视图添加到window上
//            UIApplication.shared.keyWindow?.addSubview(composeView)
            
            if let weakSelf = self {
                
                let composeView = VVComposeView()
                
                //显示罩层
                composeView.show(target: weakSelf)
                
            }
        
        }
        
        //添加到tabBar视图
        self.setValue(vvTabBar, forKey: "tabBar")
        
        addChildViewController(childController: VVHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        
        addChildViewController(childController: VVMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
 
        addChildViewController(childController: VVDiscoberTableViewController(), imageName: "tabbar_discover", title: "发现")
      
        addChildViewController(childController: VVMineTableViewController(), imageName: "tabbar_profile", title: "我的")
        
    }
    
    
    func addChildViewController(childController: UIViewController, imageName: String, title: String) {
        
        //设置tabbarItem图片
        childController.tabBarItem.image = UIImage(named: imageName)
        
        //设置tabbatItem选中图片
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        //设置文字颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor .orange], for: .selected)
        
        //设置文字大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 13)], for: .normal)
        
        //设置tabbatItem的文字
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        
         //MARK:---bug解决 点击push获取上一级控制器 title要这样设置
        childController.title = title
        
        //创建导航控制器
        let nav = VVNavigationViewController(rootViewController: childController)
        
        //添加子视图控制器
        addChildViewController(nav)
        
    }

    //MARK: ---代理方法的实现
    func didSelectedComposeButtonOperation() {
        
        print("我是代理调用过来的")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
